﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KDGAPI=KDSugar.Galaxy.API;
using Newtonsoft.Json;

namespace KDSG.API.TestCase
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private KDGAPI.GalaxyCloudAPIClient apiClient { get; set; }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            apiClient = new KDGAPI.GalaxyCloudAPIClient(tbUrl.Text, tbAcctId.Text, tbUser.Text, tbPassword.Text);
            apiClient.Login();
            if (apiClient.IsLogin)
            {
                MessageBox.Show("登录成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(!string.IsNullOrWhiteSpace(apiClient.LoginMessage) ? apiClient.LoginMessage : "登录失败，未知错误",
                    "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.Query.QueryCommand<Query.QueryModel> queryCommand = 
                    new KDGAPI.Query.QueryCommand<Query.QueryModel>(apiClient);
                queryCommand.SetFilter(m => m.Id >= 104960 ||
                    (m.Id < 104960 && m.Number == "CK002"));
                queryCommand.AppendOrderBy(m => m.Id, KDGAPI.Constant.OrderTypeEnum.Desc);
                var resultObj = queryCommand.ExecuteQueryBill();
                Console.WriteLine(queryCommand.RequestContent);
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        /// <summary>
        /// 检查是否登录
        /// </summary>
        /// <returns></returns>
        private bool CheckAPIClientLogin()
        {
            bool result = false;
            if (apiClient == null)
            {
                MessageBox.Show("请先登录账套", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (apiClient != null && !apiClient.IsLogin)
            {
                MessageBox.Show(string.Format("登录失败[{0}-{1}]|{2}", apiClient.LoginResultTypeCode,
                    Enum.GetName(typeof(KDGAPI.GalaxyCloudAPIClient.LoginResponseInfo.LoginResultTypeStatusEnum), apiClient.LoginResultTypeStatus),
                    !string.IsNullOrWhiteSpace(apiClient.LoginMessage) ? apiClient.LoginMessage : "未知错误"));
            }
            else
            {
                result = true;
            }
            return result;
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.View.ViewCommand viewCommand = new KDGAPI.View.ViewCommand("BD_STOCK", apiClient);
                viewCommand.SetNumber("CK001");
                Dictionary<string,object> resultObj = viewCommand.ExecuteViewBill();
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        private void CleanRichText()
        {
            richText.Clear();
        }

        private void btnMD_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.MetaData.MetaDataCommand metaDataCommand = new KDGAPI.MetaData.MetaDataCommand("BD_STOCK", apiClient);
                var resultObj = metaDataCommand.ExecuteMetaDataQuery();
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.SaveOrDraft.SaveCommand saveCommand = new KDGAPI.SaveOrDraft.SaveCommand(apiClient);
                Save.SaveModel saveModel = new Save.SaveModel()
                {
                    Name = "测试用例仓库"
                };
                var resultObj = saveCommand.ExecuteSaveBill(saveModel);
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.Submit.SubmitCommand submitCommand = new KDGAPI.Submit.SubmitCommand("BD_STOCK", apiClient);
                submitCommand.SetNumbers(new List<string>() { "CK019" });
                var resultObj = submitCommand.Execute();
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.AuditOrUnAudit.AuditCommand auditCommand = new KDGAPI.AuditOrUnAudit.AuditCommand("BD_STOCK", apiClient);
                auditCommand.SetNumbers(new List<string>() { "CK019" });
                var resultObj = auditCommand.Execute();
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        private void btnUnAudit_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.AuditOrUnAudit.UnAuditCommand unAuditCommand = new KDGAPI.AuditOrUnAudit.UnAuditCommand("BD_STOCK", apiClient);
                unAuditCommand.SetNumbers(new List<string>() { "CK019" });
                var resultObj = unAuditCommand.Execute();
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.Delete.DeleteCommand deleteCommand = new KDGAPI.Delete.DeleteCommand("BD_STOCK", apiClient);
                deleteCommand.SetNumbers(new List<string>() { "CK020" });
                var resultObj = deleteCommand.Execute();
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        private void btnPush_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.Push.PushCommand pushCommand = new KDGAPI.Push.PushCommand("PRD_MO", apiClient);
                pushCommand.SetNumbers(new List<string>() { "MO000016" });
                pushCommand.RuleId = "PRD_MO2INSTOCK";
                //pushCommand.TargetFormId = "PRD_INSTOCK";
                pushCommand.IsDraftWhenSaveFail = true;
                var resultObj = pushCommand.ExecutePushBill();
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }

        private void btnDraft_Click(object sender, EventArgs e)
        {
            if (CheckAPIClientLogin())
            {
                CleanRichText();
                KDGAPI.SaveOrDraft.DraftCommand saveCommand = new KDGAPI.SaveOrDraft.DraftCommand(apiClient);
                Save.SaveModel saveModel = new Save.SaveModel()
                {
                    Name = "测试用例仓库"
                };
                var resultObj = saveCommand.ExecuteSaveBill(saveModel);
                richText.AppendText(JsonConvert.SerializeObject(resultObj));
            }
        }
    }
}
